#### TABLES H.13-H.20: FULL MODEL RESULTS - NEGATIVE CONSEQUENCES
#### Complete regression tables for negative consequence outcomes

rm(list = ls())
source("./2_code/00_setup.R")
library(texreg)  # Must load after estimatr

#### HELPER FUNCTION ####

extract_models <- function(model_list) {
  output_list <- list()
  for (model in model_list) {
    extracted_model <- extract(model,
                               include.rsquared = FALSE,
                               include.adjrs = FALSE,
                               include.rmse = FALSE,
                               include.ci = FALSE,
                               include.nobs = TRUE)
    output_list <- c(output_list, list(extracted_model))
  }
  return(output_list)
}


#### STUDY 1 ####

data <- fread(paste0(data_path, "data_study1.csv"), header = TRUE)
data$worked <- as.factor(data$worked)

# New Conflict Index
lin_conflict_economic1 <- lm_lin(new_conflict_s ~ treat_economic, covariates = ~
                                   edad + worked + male, data = data[(treat=='economic' | treat=='control')])
lin_conflict_humanitarian1 <- lm_lin(new_conflict_s ~ treat_humanitarian, covariates = ~
                                       edad + worked + male, data = data[(treat=='humanitarian' | treat=='control')])

# Welfare
lin_welfare_economic1 <- lm_lin(immigrant_welfare ~ treat_economic, covariates = ~
                                  edad + worked + male, data = data[(treat=='economic' | treat=='control')])
lin_welfare_humanitarian1 <- lm_lin(immigrant_welfare ~ treat_humanitarian, covariates = ~
                                      edad + worked + male, data = data[(treat=='humanitarian' | treat=='control')])

# Crime
lin_crime_economic1 <- lm_lin(immigrant_crime ~ treat_economic, covariates = ~
                                edad + worked + male, data = data[(treat=='economic' | treat=='control')])
lin_crime_humanitarian1 <- lm_lin(immigrant_crime ~ treat_humanitarian, covariates = ~
                                    edad + worked + male, data = data[(treat=='humanitarian' | treat=='control')])

# Employment
lin_employ_economic1 <- lm_lin(immigrant_employment ~ treat_economic, covariates = ~
                                 edad + worked + male, data = data[(treat=='economic' | treat=='control')])
lin_employ_humanitarian1 <- lm_lin(immigrant_employment ~ treat_humanitarian, covariates = ~
                                     edad + worked + male, data = data[(treat=='humanitarian' | treat=='control')])


#### STUDY 2 ####

data2 <- fread(paste0(data_path, "data_study2.csv"), header = TRUE)
data2$worked <- as.factor(data2$worked)

# New Conflict Index
lin_conflict_economic2 <- lm_lin(new_conflict_s ~ treat_economic, covariates = ~
                                   edad + worked + male, data = data2[(treat=='economic' | treat=='control')])
lin_conflict_humanitarian2 <- lm_lin(new_conflict_s ~ treat_humanitarian, covariates = ~
                                       edad + worked + male, data = data2[(treat=='humanitarian' | treat=='control')])

# Welfare
lin_welfare_economic2 <- lm_lin(immigrant_welfare ~ treat_economic, covariates = ~
                                  edad + worked + male, data = data2[(treat=='economic' | treat=='control')])
lin_welfare_humanitarian2 <- lm_lin(immigrant_welfare ~ treat_humanitarian, covariates = ~
                                      edad + worked + male, data = data2[(treat=='humanitarian' | treat=='control')])

# Crime
lin_crime_economic2 <- lm_lin(immigrant_crime ~ treat_economic, covariates = ~
                                edad + worked + male, data = data2[(treat=='economic' | treat=='control')])
lin_crime_humanitarian2 <- lm_lin(immigrant_crime ~ treat_humanitarian, covariates = ~
                                    edad + worked + male, data = data2[(treat=='humanitarian' | treat=='control')])

# Employment
lin_employ_economic2 <- lm_lin(immigrant_employment ~ treat_economic, covariates = ~
                                 edad + worked + male, data = data2[(treat=='economic' | treat=='control')])
lin_employ_humanitarian2 <- lm_lin(immigrant_employment ~ treat_humanitarian, covariates = ~
                                     edad + worked + male, data = data2[(treat=='humanitarian' | treat=='control')])


#### STUDY 3, WAVE 1 ####

data3 <- fread(paste0(data_path, "data_study3.csv"), header = TRUE)
data3$employed <- as.factor(data3$employed)

# New Conflict Index
lin_conflict_economic3 <- lm_lin(new_conflict_s ~ treat_economic, covariates = ~
                                   edad + employed + male, data = data3[(treat=='economic' | treat=='control')])
lin_conflict_humanitarian3 <- lm_lin(new_conflict_s ~ treat_humanitarian, covariates = ~
                                       edad + employed + male, data = data3[(treat=='humanitarian' | treat=='control')])

# Welfare
lin_welfare_economic3 <- lm_lin(immigrant_welfare ~ treat_economic, covariates = ~
                                  edad + employed + male, data = data3[(treat=='economic' | treat=='control')])
lin_welfare_humanitarian3 <- lm_lin(immigrant_welfare ~ treat_humanitarian, covariates = ~
                                      edad + employed + male, data = data3[(treat=='humanitarian' | treat=='control')])

# Crime
lin_crime_economic3 <- lm_lin(immigrant_crime ~ treat_economic, covariates = ~
                                edad + employed + male, data = data3[(treat=='economic' | treat=='control')])
lin_crime_humanitarian3 <- lm_lin(immigrant_crime ~ treat_humanitarian, covariates = ~
                                    edad + employed + male, data = data3[(treat=='humanitarian' | treat=='control')])

# Employment
lin_employ_economic3 <- lm_lin(immigrant_employment ~ treat_economic, covariates = ~
                                 edad + employed + male, data = data3[(treat=='economic' | treat=='control')])
lin_employ_humanitarian3 <- lm_lin(immigrant_employment ~ treat_humanitarian, covariates = ~
                                     edad + employed + male, data = data3[(treat=='humanitarian' | treat=='control')])


#### STUDY 3, WAVE 2 ####

data3b <- fread(paste0(data_path, "data_study3_wave2.csv"), header = TRUE)
data3b$employed <- as.factor(data3b$employed)

# New Conflict Index
lin_conflict_economic3b <- lm_lin(new_conflict_s ~ treat_economic, covariates = ~
                                    edad + employed + male, data = data3b[(treat=='economic' | treat=='control')])
lin_conflict_humanitarian3b <- lm_lin(new_conflict_s ~ treat_humanitarian, covariates = ~
                                        edad + employed + male, data = data3b[(treat=='humanitarian' | treat=='control')])

# Welfare
lin_welfare_economic3b <- lm_lin(immigrant_welfare ~ treat_economic, covariates = ~
                                   edad + employed + male, data = data3b[(treat=='economic' | treat=='control')])
lin_welfare_humanitarian3b <- lm_lin(immigrant_welfare ~ treat_humanitarian, covariates = ~
                                       edad + employed + male, data = data3b[(treat=='humanitarian' | treat=='control')])

# Crime
lin_crime_economic3b <- lm_lin(immigrant_crime ~ treat_economic, covariates = ~
                                 edad + employed + male, data = data3b[(treat=='economic' | treat=='control')])
lin_crime_humanitarian3b <- lm_lin(immigrant_crime ~ treat_humanitarian, covariates = ~
                                     edad + employed + male, data = data3b[(treat=='humanitarian' | treat=='control')])

# Employment
lin_employ_economic3b <- lm_lin(immigrant_employment ~ treat_economic, covariates = ~
                                  edad + employed + male, data = data3b[(treat=='economic' | treat=='control')])
lin_employ_humanitarian3b <- lm_lin(immigrant_employment ~ treat_humanitarian, covariates = ~
                                      edad + employed + male, data = data3b[(treat=='humanitarian' | treat=='control')])


#### EXTRACT MODEL INFO ####

output_list1_econ <- extract_models(list(lin_conflict_economic1, lin_welfare_economic1,
                                         lin_crime_economic1, lin_employ_economic1))

output_list1_hum <- extract_models(list(lin_conflict_humanitarian1, lin_welfare_humanitarian1,
                                        lin_crime_humanitarian1, lin_employ_humanitarian1))

output_list2_econ <- extract_models(list(lin_conflict_economic2, lin_welfare_economic2,
                                         lin_crime_economic2, lin_employ_economic2))

output_list2_hum <- extract_models(list(lin_conflict_humanitarian2, lin_welfare_humanitarian2,
                                        lin_crime_humanitarian2, lin_employ_humanitarian2))

output_list3_w1_econ <- extract_models(list(lin_conflict_economic3, lin_welfare_economic3,
                                            lin_crime_economic3, lin_employ_economic3))

output_list3_w1_human <- extract_models(list(lin_conflict_humanitarian3, lin_welfare_humanitarian3,
                                             lin_crime_humanitarian3, lin_employ_humanitarian3))

output_list3_w2_econ <- extract_models(list(lin_conflict_economic3b, lin_welfare_economic3b,
                                            lin_crime_economic3b, lin_employ_economic3b))

output_list3_w2_human <- extract_models(list(lin_conflict_humanitarian3b, lin_welfare_humanitarian3b,
                                             lin_crime_humanitarian3b, lin_employ_humanitarian3b))


#### COEFFICIENT LABELS ####

# Studies 1 & 2
model_names_1_2 <- c("Neg. Cons. Index", "Welfare", "Crime", "Employment")

coeff_names_1_2 <- list(
  "treat_economic" = "Economic Treatment",
  "treat_humanitarian" = "Humanitarian Treatment",
  "edad_c" = "Age",
  "workedPrefiero no decir_c" = "Working NT",
  "workedSí_c" = "Working",
  "male_c" = "Male",
  "treat_economic:edad_c" = "Economic Treatment x Age",
  "treat_economic:workedPrefiero no decir_c" = "Economic Treatment x Working NT",
  "treat_economic:workedSí_c" = "Economic Treatment x Working",
  "treat_economic:male_c" = "Economic Treatment x Male",
  "treat_humanitarian:edad_c" = "Humanitarian Treatment x Age",
  "treat_humanitarian:workedPrefiero no decir_c" = "Humanitarian Treatment x Working NT",
  "treat_humanitarian:workedSí_c" = "Humanitarian Treatment x Working",
  "treat_humanitarian:male_c" = "Humanitarian Treatment x Male",
  "(Intercept)" = "Intercept"
)

# Study 3
model_names_3 <- c("Neg. Cons. Index", "Welfare", "Crime", "Employment")

coeff_names_3 <- list(
  "treat_economic" = "Economic Treatment",
  "treat_humanitarian" = "Humanitarian Treatment",
  "edad_c" = "Age",
  "employedEstoy incapacitado permanentemente para trabajar_c" = "Disability",
  "employedEstoy jubilado o pensionado_c" = "Retired",
  "employedMe dedico a los quehaceres de mi hogar_c" = "Domestic",
  "employedNo estoy trabajando en este momento pero tengo trabajo_c" = "Looking",
  "employedNo trabajo y no estoy buscando trabajo_c" = "Not Looking",
  "employedSoy estudiante_c" = "Student",
  "employedTrabajando_c" = "Working",
  "male_c" = "Male",
  "treat_economic:edad_c" = "Economic Treatment x Age",
  "treat_economic:employedEstoy incapacitado permanentemente para trabajar_c" = "Economic Treatment x Disability",
  "treat_economic:employedEstoy jubilado o pensionado_c" = "Economic Treatment x Retired",
  "treat_economic:employedMe dedico a los quehaceres de mi hogar_c" = "Economic Treatment x Domestic",
  "treat_economic:employedNo estoy trabajando en este momento pero tengo trabajo_c" = "Economic Treatment x Looking",
  "treat_economic:employedNo trabajo y no estoy buscando trabajo_c" = "Economic Treatment x Not Looking",
  "treat_economic:employedSoy estudiante_c" = "Economic Treatment x Student",
  "treat_economic:employedTrabajando_c" = "Economic Treatment x Working",
  "treat_economic:male_c" = "Economic Treatment x Male",
  "treat_humanitarian:edad_c" = "Humanitarian Treatment x Age",
  "treat_humanitarian:employedEstoy incapacitado permanentemente para trabajar_c" = "Humanitarian Treatment x Disability",
  "treat_humanitarian:employedEstoy jubilado o pensionado_c" = "Humanitarian Treatment x Retired",
  "treat_humanitarian:employedMe dedico a los quehaceres de mi hogar_c" = "Humanitarian Treatment x Domestic",
  "treat_humanitarian:employedNo estoy trabajando en este momento pero tengo trabajo_c" = "Humanitarian Treatment x Looking",
  "treat_humanitarian:employedNo trabajo y no estoy buscando trabajo_c" = "Humanitarian Treatment x Not Looking",
  "treat_humanitarian:employedSoy estudiante_c" = "Humanitarian Treatment x Student",
  "treat_humanitarian:employedTrabajando_c" = "Humanitarian Treatment x Working",
  "treat_humanitarian:male_c" = "Humanitarian Treatment x Male",
  "(Intercept)" = "Intercept"
)


#### EXPORT TO LATEX ####

# Table H.13: Study 1 Economic
latex_output <- texreg(
  output_list1_econ,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_1_2,
  custom.coef.map = coeff_names_1_2,
  caption = "Full Model Results of Study 1: Negative Consequences (Economic Treatment)",
  caption.above = TRUE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H13.tex"))

# Table H.14: Study 1 Humanitarian
latex_output <- texreg(
  output_list1_hum,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_1_2,
  custom.coef.map = coeff_names_1_2,
  caption = "Full Model Results of Study 1: Negative Consequences (Humanitarian Treatment)",
  caption.above = TRUE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H14.tex"))

# Table H.15: Study 2 Economic
latex_output <- texreg(
  output_list2_econ,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_1_2,
  custom.coef.map = coeff_names_1_2,
  caption = "Full Model Results of Study 2: Negative Consequences (Economic Treatment)",
  caption.above = TRUE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H15.tex"))

# Table H.16: Study 2 Humanitarian
latex_output <- texreg(
  output_list2_hum,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_1_2,
  custom.coef.map = coeff_names_1_2,
  caption = "Full Model Results of Study 2: Negative Consequences (Humanitarian Treatment)",
  caption.above = TRUE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H16.tex"))

# Table H.17: Study 3 Wave 1 Economic
latex_output <- texreg(
  output_list3_w1_econ,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_3,
  custom.coef.map = coeff_names_3,
  caption = "Full Model Results of Study 3 (Wave 1): Negative Consequences (Economic Treatment)",
  caption.above = TRUE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H17.tex"))

# Table H.18: Study 3 Wave 1 Humanitarian
latex_output <- texreg(
  output_list3_w1_human,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_3,
  custom.coef.map = coeff_names_3,
  caption = "Full Model Results of Study 3 (Wave 1): Negative Consequences (Humanitarian Treatment)",
  caption.above = TRUE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H18.tex"))

# Table H.19: Study 3 Wave 2 Economic
latex_output <- texreg(
  output_list3_w2_econ,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_3,
  custom.coef.map = coeff_names_3,
  caption = "Full Model Results of Study 3 (Wave 2): Negative Consequences (Economic Treatment)",
  caption.above = TRUE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H19.tex"))

# Table H.20: Study 3 Wave 2 Humanitarian
latex_output <- texreg(
  output_list3_w2_human,
  stars = c(0.01, 0.05, 0.1),
  custom.model.names = model_names_3,
  custom.coef.map = coeff_names_3,
  caption = "Full Model Results of Study 3 (Wave 2): Negative Consequences (Humanitarian Treatment)",
  caption.above = TRUE,
  return.string = TRUE
)
writeLines(latex_output, paste0(tables_path, "table_H20.tex"))
